package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.WithdrawStatus;
import com.rockcent.mall.lib.dto.CustomerWithdrawDto;
import com.rockcent.mall.lib.dto.MerchantWithdrawDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * Created by DK on 16/12/13.
 */
@Repository
public class CustomerWithdrawRepositoryImpl implements CustomerWithdrawRepositoryCustom {

    private Logger log = LoggerFactory.getLogger(CustomerWithdrawRepositoryImpl.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public PageResult<CustomerWithdrawDto> findMallCustomerApplyWithdrawPage(Long customerId, Long mallId, String beginDate, String endDate, BigDecimal maxAmount, BigDecimal minAmount, String status, PageResult page) {
        SQLPageDoing<CustomerWithdrawDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from customer_withdraw cw left join customer c on cw.customer_id = c.id where 1=1 ");

            if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
                sql.append(" AND cw.date_created < :endDate AND cw.date_created > :beginDate");
                params.put("beginDate", RDateUtils.beforeDateStr(beginDate, 0));
                params.put("endDate", RDateUtils.afterDateStr(endDate, 1));
            }

            if (WithdrawStatus.FINISHED == WithdrawStatus.valueOf(status)) {
                sql.append(" and cw.status in (:status)");
                params.put("status", Arrays.asList(WithdrawStatus.FAIL.toString(), WithdrawStatus.SUCCESS.toString(), WithdrawStatus.TRANSFER_FAIL.toString()));
            }else {
                sql.append(" and cw.status = :status");
                params.put("status", status);
            }

            if (maxAmount != null && minAmount != null) {
                sql.append(" AND cw.amount <= :maxAmount AND cw.amount >= :minAmount");
                params.put("maxAmount", maxAmount);
                params.put("minAmount", minAmount);
            }

            if (customerId != null){
                sql.append(" and cw.customer_id = :customerId");
                params.put("customerId", customerId);
            }
            sql.append(" and cw.mall_id = :mallId");
            params.put("mallId", mallId);

            sql.append(" order by cw.date_created desc");
            fields.append("cw.*, c.nick_name customerName");
        };

        return doing.go(page, CustomerWithdrawDto.class, objectMapper, jdbcTemplate, log);
    }
}
